package com.hds.modules.job.task;

import com.hds.modules.app.entity.ActivationCodeEntity;
import com.hds.modules.app.entity.UserEntity;
import com.hds.modules.app.enums.ActivateCode;
import com.hds.modules.app.enums.UserEnum;
import com.hds.modules.app.service.ActivationCodeService;
import com.hds.modules.app.service.UserService;
import com.hds.modules.job.entity.ScheduleJobEntity;
import com.hds.modules.job.service.ScheduleJobService;
import com.hds.modules.sys.entity.SysUserEntity;
import com.hds.modules.sys.entity.base.SysMemberShipEntity;
import com.hds.modules.sys.service.base.SysMemberShipService;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Array;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author guonan
 * @Email guonan@yewcn.com
 * @Description
 * @Date 2018/6/12 17:08
 */
@Component("activateCodeTask")
public class ActivateCodeTask {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ActivationCodeService activationCodeService;

    @Autowired
    private UserService userService;

    @Autowired
    private SysMemberShipService sysMemberShipService;

    @Autowired
    private com.hds.modules.job.service.ScheduleJobService scheduleJobService;

    /**
     * 激活码：单个已激活的激活码，到期后修改状态为-已失效
     * @param id 激活码id
     */
    @Transactional
    public void activateExpire(String id){
        logger.info("我是带参数的activateExpire方法，正在被执行，参数为：" + id);

        ActivationCodeEntity code = activationCodeService.selectById(Integer.valueOf(id));
        UserEntity user = userService.selectById(code.getUid());
        logger.info(">>level:{}",UserEnum.VipLevel.TOURIST.getLevel());
        SysMemberShipEntity ship = sysMemberShipService.queryByLevel(UserEnum.VipLevel.TOURIST.getLevel());
        logger.info(">>ship:{}",ship);
        if(code!=null){
            code.setStatus(ActivateCode.StatusEnum.EXPIRED.getStatus());
            boolean r = activationCodeService.insertOrUpdate(code);
            user.setLevelId(ship.getId());
            boolean b = userService.insertOrUpdate(user);
            if(b){
                //将该定时器设置为暂停
                Map<String, Object> map = new HashMap<>();
                map.put("params", id);
                List<ScheduleJobEntity> list =  scheduleJobService.selectByMap(map);
                if(list!=null && list.size()>0){
                    Long[] ids = {list.get(0).getJobId()};
                    scheduleJobService.pause(ids);
                    System.out.println("定时器参数为"+id+"过期删除成功");
                }
            }
        }

    }

    /**
     * 激活码：超过保质期，该公司(companyId)的这一批次(batch)未激活的激活码全部失效
     */
    public void batchExpire(String companyIdAndBatch){
        logger.info("超过保质期，未激活的激活码全部失效");

        String companyId = companyIdAndBatch.substring(0,companyIdAndBatch.indexOf("|"));
        String batch = companyIdAndBatch.substring(companyIdAndBatch.indexOf("|")+1,companyIdAndBatch.length());

        Map<String, Object> map = new HashMap<>();
        map.put("status", ActivateCode.StatusEnum.ENABLED.getStatus());
        map.put("company_id",companyId);
        map.put("batch", batch);
        List<ActivationCodeEntity> list = activationCodeService.selectByMap(map);

        if(list!=null && list.size()>0){
            for (ActivationCodeEntity code : list) {
                code.setStatus(ActivateCode.StatusEnum.EXPIRED.getStatus());
                activationCodeService.insertOrUpdate(code);
            }

        }

    }
}
